package com.leyou.item.controller;

import com.leyou.item.interfaces.pojo.Brand;
import com.leyou.item.service.BrandService;
import com.leyoucommon.pojo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品品牌controller
 * @version 1.0.0
 * @Author yong.Mr
 * @data 2019-8-9
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/brand")
public class BrandController {

    /**
     * 注入BrandService
     */
    @Autowired
    private BrandService brandService;

    /**
     * 根据查询条件分页并排序查询品牌信息
     * @param key 模糊查询的条件
     * @param page 当前页数
     * @param rows 当前页的数据量
     * @param sortBy 需要排序的字段
     * @param desc 是否降序
     * @return PageResult<Brand>
     */
    @GetMapping("/page")
    public ResponseEntity<PageResult<Brand>> queryBrandByPage(
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "page",required = false,defaultValue = "1")Integer page,
            @RequestParam(value = "rows",required = false,defaultValue = "5")Integer rows,
            @RequestParam(value = "sortBy",required = false)String sortBy,
            @RequestParam(value = "desc",required = false)Boolean desc){

        PageResult<Brand> result = brandService.queryBrandByPageSql(key, page, rows, sortBy, desc);

        if(CollectionUtils.isEmpty(result.getItems())){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(result);
    }

    /**
     * 新增品牌
     * @param brand 品牌参数
     * @param cids 分类的id
     * @return ResponseEntity<Void>
     */
    @PostMapping(produces = "application/json ; charset = UTF-8")
    public ResponseEntity<Void> saveBrand(Brand brand ,@RequestParam("cids") List<Long> cids,
                                          @RequestParam("flag") Boolean flag){
        System.out.println("新增商品传入的数据----->:" + brand.toString());
        // true为修改操作
        if(flag){
            return brandService.editBrand(brand, cids);
        }
        // false为新增操作
        if(!flag) {
            return brandService.saveBrand(brand, cids);
        }
        return ResponseEntity.notFound().build();
    }

    /**
     * 查询该商品分类下的品牌
     * @param cid 商品分类的id
     * @return ResponseEntity<List<Brand>>
     */
    @RequestMapping("/cid/{cid}")
    public ResponseEntity<List<Brand>> queryBrandByCid(@PathVariable("cid") Long cid){
        List<Brand> brands = brandService.queryBrandByCid(cid);
        if(CollectionUtils.isEmpty(brands)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(brands);
    }
    /**
     * 删除品牌的相关信息
     * @param brandId 品牌id
     * @return ResponseEntity<Void>
     */
    @RequestMapping("/delete/{brandId}")
    public ResponseEntity<Void> deleteBrand(@PathVariable("brandId") Long brandId){
        System.out.println("删除用户----->:" + brandId);
        return brandService.deleteBrand(brandId);
    }

    @GetMapping("{id}")
    public ResponseEntity<Brand>  queryBrandById(@PathVariable("id")Long id){
        Brand brand = this.brandService.queryBrandById(id);
        System.out.println("查询到数据了吗：" + brand);
        if(brand == null){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(brand);
    }

}
